package org.csu.mypetstorebackstage.controller;

import org.csu.mypetstorebackstage.domain.Product;
import org.csu.mypetstorebackstage.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/onshelf")
    public String showOnshelf(Model model, HttpSession session) {
        List<Product> onSellProductList = categoryService.showOnSellProduct();
        List<Product> outShelfProductList = categoryService.showOutShelfProduct();
        for(int i= 1; i<=onSellProductList.size();i++) {
            int number = categoryService.getItemNumberByProductId(onSellProductList.get(i-1).getProductId());
            onSellProductList.get(i-1).setNumber(number);
        }

        for(int i= 1; i<=outShelfProductList.size(); i++) {
            int number = categoryService.getItemNumberByProductId(outShelfProductList.get(i-1).getProductId());
            outShelfProductList.get(i-1).setNumber(number);
        }
        model.addAttribute("onSellProductList", onSellProductList);

        model.addAttribute("outShelfProductList", outShelfProductList);

//        if (outShelfProductList != null) {
//            System.out.println("查询成功！！！！！");
//            System.out.println("++++++++" + outShelfProductList.size());
//            for (Product product : outShelfProductList) {
//                System.out.println(product.getProductId()+"===="+product.getNumber());
//            }
//        }
        return "category/isOnsell";
    }

    @RequestMapping("/updateOnShelf")
    public String updateOnShelf(Model model,String productId){
        System.out.println("访问成功！！！！！！");
        categoryService.updateOnShelf(productId);
        List<Product> onSellProductList = categoryService.showOnSellProduct();
        List<Product> outShelfProductList = categoryService.showOutShelfProduct();
        for(int i= 1; i<=onSellProductList.size();i++) {
            int number = categoryService.getItemNumberByProductId(onSellProductList.get(i-1).getProductId());
            onSellProductList.get(i-1).setNumber(number);
        }

        for(int i= 1; i<=outShelfProductList.size(); i++) {
            int number = categoryService.getItemNumberByProductId(outShelfProductList.get(i-1).getProductId());
            outShelfProductList.get(i-1).setNumber(number);
        }
        model.addAttribute("onSellProductList", onSellProductList);

        model.addAttribute("outShelfProductList", outShelfProductList);
        return "category/isOnsell";
    }

    @RequestMapping("updateOutShelf")
    public String updateOutShelf(Model model,String productId){
        System.out.println("访问成功！！！！！！");
        categoryService.updateOutShelf(productId);
        List<Product> onSellProductList = categoryService.showOnSellProduct();
        List<Product> outShelfProductList = categoryService.showOutShelfProduct();
        for(int i= 1; i<=onSellProductList.size();i++) {
            int number = categoryService.getItemNumberByProductId(onSellProductList.get(i-1).getProductId());
            onSellProductList.get(i-1).setNumber(number);
        }

        for(int i= 1; i<=outShelfProductList.size(); i++) {
            int number = categoryService.getItemNumberByProductId(outShelfProductList.get(i-1).getProductId());
            outShelfProductList.get(i-1).setNumber(number);
        }
        model.addAttribute("onSellProductList", onSellProductList);

        model.addAttribute("outShelfProductList", outShelfProductList);
        return "category/isOnsell";
    }

}
